package com.argrace.smart.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.aliyun.alink.business.alink.ALinkBusinessEx;
import com.aliyun.alink.sdk.net.anet.api.AError;
import com.aliyun.alink.sdk.net.anet.api.transitorynet.TransitoryRequest;
import com.aliyun.alink.sdk.net.anet.api.transitorynet.TransitoryResponse;
import com.argrace.smart.R;
import com.argrace.smart.base.BaseActivity;
import com.argrace.smart.base.ConstantConfigure;
import com.argrace.smart.entity.WangGuanDeviceEntity;
import com.argrace.smart.model.LocalDb_DeviceNameAndRoomNameTable;
import com.argrace.smart.util.AlinkHttpUtil;

import java.util.HashMap;
import java.util.List;

/**
 * Created by argrace_dev_1 on 2018/3/2.
 */

public class CreateMyDeviceActivity extends BaseActivity {
    private EditText deviceName;
    private TextView devicePosition, relationDevice;
    private LinearLayout devicePosition_ll, relationDevice_ll;
    private String relationDevice_str, relationDevice_model;
    private int devicePosition_id = 0;
    private final int REQUEST_DEVICEPOSITION = 301;
    private final int REQUEST_RELAITIONDEVICE = 302;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_createmydevice);
        init();
    }

    private void init() {
        setView();
        setTitle(true, true, "添加设备");
        //进入设备修改保存状态界面
        showRight(true, R.mipmap.head_title_tick);
        setLeftandRightClickListeners(null, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }

        });
    }

    private void save() {
        LocalDb_DeviceNameAndRoomNameTable deviceBean = new LocalDb_DeviceNameAndRoomNameTable();
        deviceBean.setNickName(deviceName.getText().toString());
        deviceBean.setRoomName(devicePosition.getText().toString());
        deviceBean.setModel(relationDevice_model);
        if (relationDevice_str.split("-") != null && relationDevice_str.split("-").length == 2) {
            deviceBean.setUuid(relationDevice_str.split("-")[0]);
            deviceBean.setProperty(relationDevice_str.split("-")[1]);
            deviceBean.setDeviceKey(relationDevice_str);
            int result = deviceBean.updateAll("uuid = ? and property = ?", relationDevice_str.split("-")[0], relationDevice_str.split("-")[1]);
            if (result == 0) {
                deviceBean.save();
            }
        } else {
            deviceBean.setUuid(relationDevice_str);
            //针对直接控制设备的，则再其devicekey上拼接一个Switch代表控制键位
            deviceBean.setDeviceKey(relationDevice_str + "-" + "Switch");
            int result = deviceBean.updateAll("uuid = ?", relationDevice_str);
            if (result == 0) {
                deviceBean.save();
            }
        }
        finish();
    }

    private void setView() {
        devicePosition_ll = (LinearLayout) findViewById(R.id.activity_createmydevice_deviceposition_ll);
        relationDevice_ll = (LinearLayout) findViewById(R.id.activity_createmydevice_relationdevice_ll);
        devicePosition = (TextView) findViewById(R.id.activity_createmydevice_deviceposition);
        relationDevice = (TextView) findViewById(R.id.activity_createmydevice_relationdevice);
        deviceName = (EditText) findViewById(R.id.activity_createmydevice_devicename);
        devicePosition_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CreateMyDeviceActivity.this, MyRoomActivity.class);
                startActivityForResult(intent, REQUEST_DEVICEPOSITION);
            }
        });
        relationDevice_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getMyDevice();
            }
        });
    }

    private void getMyDevice() {
        String method = "mtop.openalink.app.core.devices.getbyuser";
        AlinkHttpUtil.AlinkHttpRequest(method, new HashMap<String, Object>(), new ALinkBusinessEx.IListener() {
            @Override
            public void onSuccess(TransitoryRequest transitoryRequest, TransitoryResponse transitoryResponse) {
                if (transitoryResponse != null && transitoryResponse.data != null) {
                    List<WangGuanDeviceEntity> beans = JSON.parseArray((String) transitoryResponse.data, WangGuanDeviceEntity.class);
                    if (beans != null && beans.size() > 0) {
                        Intent intent = new Intent(CreateMyDeviceActivity.this, RuoQi_SubDeviceListActivity.class);
                        intent.putExtra("uuid", beans.get(0).getUuid());
                        startActivityForResult(intent, REQUEST_RELAITIONDEVICE);
                    }
                }
            }

            @Override
            public void onFailed(TransitoryRequest transitoryRequest, AError aError) {
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null && resultCode == ConstantConfigure.RESULT_SUCCESS) {
            switch (requestCode) {
                case REQUEST_RELAITIONDEVICE:
                    String result = data.getStringExtra("result");
                    relationDevice_model = data.getStringExtra("model");
                    String[] relationdevice_arr = result.split(":");
                    if (relationdevice_arr != null && relationdevice_arr.length == 2) {
                        relationDevice.setText(relationdevice_arr[0]);
                        relationDevice_str = relationdevice_arr[1];
                    }
                    break;
                case REQUEST_DEVICEPOSITION:
                    String name = data.getStringExtra("name");
                    devicePosition_id = data.getIntExtra("id", 0);
                    devicePosition.setText(name);
                    break;
            }
        }


    }
}
